﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	AreaDialog
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class AreaDialog : Form
    {
        public AreaDialog()
         {
              InitializeComponent();
              dataGridView1.ColumnCount = 7;
              this.dataGridView1.Columns[0].HeaderText = "codice";
              this.dataGridView1.Columns[1].HeaderText = "denominazione";
              this.dataGridView1.Columns[2].HeaderText = "codice bordo";
              this.dataGridView1.Columns[3].HeaderText = "bordo";
              this.dataGridView1.Columns[4].HeaderText = "codice camp.";
              this.dataGridView1.Columns[5].HeaderText = "campitura";
              this.dataGridView1.Columns[6].HeaderText = "colore";

              this.dataGridView1.Columns[0].Width = 70;
              this.dataGridView1.Columns[1].Width = 280;
              this.dataGridView1.Columns[2].Width = 50;
              this.dataGridView1.Columns[3].Width = 120;
              this.dataGridView1.Columns[4].Width = 50;
              this.dataGridView1.Columns[5].Width = 250;
              this.dataGridView1.Columns[6].Width = 60;

              dataGridView1.AllowUserToAddRows = false;
              dataGridView1.AllowUserToDeleteRows = false;

              int iSel = 0;
              for (int i = 0; i < ViewQ.nSimbA; ++i)
              {
                   if (ViewQ.vectA[i].selected == 0) continue;
                   string[] row = new string[] {
                     ViewQ.vectA[i].codice.ToString(),ViewQ.vectA[i].denom.ToString(),
                ViewQ.vectA[i].codice_bordo.ToString(),ViewQ.vectA[i].bordo,
                ViewQ.vectA[i].codice_camp.ToString(),ViewQ.vectA[i].campitura};
                   dataGridView1.Rows.Add(row);
                   dataGridView1[6, iSel].Style.BackColor = ViewQ.vectA[i].icolor;
                   ++iSel;
              }

              textBox1.Text = iSel.ToString();

              button1.DialogResult = DialogResult.Cancel;
              button2.DialogResult = DialogResult.OK;

              DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
              buttonColumn.HeaderText = "";
              buttonColumn.Name = "Color";
              buttonColumn.Text = "V";
              buttonColumn.UseColumnTextForButtonValue = true;
              buttonColumn.Width = 20;

              dataGridView1.Columns.Insert(7, buttonColumn);
              // Add a CellClick handler to handle clicks in the button column.
              dataGridView1.CellClick += new DataGridViewCellEventHandler(this.dataGridView1_CellClick);

              DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
              {
                   column.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
                   column.FlatStyle = FlatStyle.Standard;
                   column.ThreeState = false;
                   column.CellTemplate = new DataGridViewCheckBoxCell();
                   column.CellTemplate.Style.BackColor = Color.White;
                   column.HeaderText = "visibile";
              }
              int iCol = 8;
              dataGridView1.Columns.Insert(iCol, column);

              iSel = 0;
              for (int i = 0; i < ViewQ.nSimbA; ++i)
              {
                   if (ViewQ.vectA[i].selected == 0) continue;
                   if (ViewQ.vectA[i].visib == 0) dataGridView1[iCol, iSel].Value = false;
                   else
                   {
                        dataGridView1[iCol, iSel].Value = true;
                   }
                   ++iSel;
              }

              DataGridViewCheckBoxColumn column1 = new DataGridViewCheckBoxColumn();
              {
                   //        column.HeaderText = ColumnName.OutOfOffice.ToString();
                   //      column.Name = ColumnName.OutOfOffice.ToString();
                   column1.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
                   //      DataGridViewAutoSizeColumnMode.DisplayedCells;
                   column1.FlatStyle = FlatStyle.Standard;
                   column1.ThreeState = false;
                   column1.CellTemplate = new DataGridViewCheckBoxCell();
                   column1.CellTemplate.Style.BackColor = Color.White;
                   column1.HeaderText = "presente in archivio";
              }

              iCol = 9;
              dataGridView1.Columns.Insert(iCol, column1);

              using (SQLiteConnection conn = SetConnection())
              {
                   conn.Open();
                   for (int i = 0; i < ViewQ.nSimbA; ++i)
                   {
                        if (ViewQ.vectA[i].selected == 0) continue;

                        using (SQLiteCommand cmd = new SQLiteCommand(conn))
                        {
                             cmd.CommandText = "select * from area where codice_area = '" + ViewQ.vectA[i].codice.ToString() + "'";

                             SQLiteDataReader reader = cmd.ExecuteReader();
                             if (reader.HasRows) dataGridView1[iCol, i].Value = true;
                        }
                   }
                   conn.Close();
              }
         }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 7)
            {
                ColorDialog colorDialog1 = new ColorDialog();
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    dataGridView1[6, e.RowIndex].Style.BackColor = colorDialog1.Color;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int count = dataGridView1.Rows.Count;

            for (int i = 0; i < count; ++i)
            {
                string row = dataGridView1[0, i].Value.ToString();
                for (int j = 0; j < ViewQ.nSimbA; ++j)
                {
                    int comp = string.Compare(ViewQ.vectA[i].elemento, 0, ViewQ.selectedElement, 0, 6);
                    comp = 0;
                    if (comp == 0)
                    {
                        if (ViewQ.vectA[j].codice.Contains(row))
                        {
                            ViewQ.vectA[j].icolor = dataGridView1[6, i].Style.BackColor;
                            ViewQ.vectA[j].codice_bordo = int.Parse(dataGridView1[2, i].Value.ToString());
                            ViewQ.vectA[j].bordo = dataGridView1[3, i].Value.ToString();
                            ViewQ.vectA[j].codice_camp = int.Parse(dataGridView1[4, i].Value.ToString());
                            ViewQ.vectA[j].campitura = dataGridView1[5, i].Value.ToString();

             
                            if ((bool)dataGridView1[8, i].Value) ViewQ.vectA[j].visib = 1;
                            else ViewQ.vectA[j].visib = 0;
                            break;
                        }
                    }
                }
            }

            this.Close();
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
            int count = this.dataGridView1.RowCount;
            bool bChecked = (bool)checkBox1.Checked;
            if (bChecked)
            {
                for (int j = 0; j < count; ++j)
                {
                    dataGridView1[8, j].Value = false;
                }
            }
            else
            {
                for (int j = 0; j < count; ++j)
                {
                    dataGridView1[8, j].Value = true;
                }
            }

            dataGridView1.Refresh();
        }

 
        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private string aggiungiApostrofo(string orig)
        {
            string s1 = "";
            string s2 = ""; string s3 = "";
            int ipos = 0, len = 0;
            int ipos1 = 0;
            s2 = orig;

        L10: ipos = s2.IndexOf('\x27', ipos1);

            if (ipos > 0)
            {
                len = s2.Length;
                s1 = s2.Substring(0, ipos);
                s1 = string.Concat(s1, "\x27\x27");
                s3 = s2.Substring(ipos + 1, len - ipos - 1);
                s1 = string.Concat(s1, s3);
                s2 = s1;
                ipos1 = ipos + 2;
                goto L10;
            }

            return (s2);
        }




    }
}
